# coding: utf-8
# 文件名称: uibot前奏.py
# 创建时间: 2021/7/1 11:13

# coding: utf-8
# 文件名称: json解析_获取商品链接.py
# 创建时间: 2021/7/1 0:03

import json
import csv
import os


# 处理原json数据
def qingnxi_data(file):
    f = open(file, 'r', encoding='utf-8')
    text = json.load(f)
    f.close()

    # print(text)

    datas = jiexi(text)

# 解析json
def jiexi(j):
    datas = j['goods_list']
    for data in datas:
        if 'goods_id' in data:
            goods_id = str(data['goods_id'])
            mall_id = str(data['mall_id'])
            jiage = float(data['price'] / 100)
        else:
            goods_id = str(data['goods_list'][0]['goods_id'])
            mall_id = str(data['goods_list'][0]['mall_id'])
            jiage = float(data['goods_list'][0]['price'] / 100)

        chuli_data(goods_id, mall_id, jiage)

lists = []
def chuli_data(goods_id, mall_id, jiage):
    # 过滤低于1元的商品
    if jiage <= 0.7:
        goods = {
            'url': f'https://mobile.yangkeduo.com/chat_detail.html?mall_id={mall_id}&goods_id={goods_id}&cat_id_1=16901&page_from=0&is_full_reduction=false&is_full_return=false&refer_page_name=goods_detail&refer_page_id=10014_1625074923468_7n5946ogyw&refer_page_sn=10014',
            'goods_id': goods_id,
            'mall_id': mall_id,
            'jiage': jiage,
            'goods_url':f'https://mobile.yangkeduo.com/goods.html?goods_id={goods_id}'
        }
        # print(goods)

        # 读取之前保存的所有商品goods_id，如果在文件中存在则不append，如果存在则添加
        OFF = True
        with open('all_goods_id.csv', 'r', encoding='utf-8') as fb:
            for f in fb.readlines():
                if str(goods_id).strip() in f.strip():
                    # print('已存在==',goods_id)
                    OFF = False
                    break
        if OFF:
            lists.append(goods)
            save_lists()

        # 存储所有小于1元的商品goods_id到本地
        save_goods_is(goods_id)

# 存储所有小于1元的商品goods_id到本地函数
def save_goods_is(goods_id):
    all_goods_id = open('all_goods_id.csv', 'a+', encoding='utf-8')
    all_goods_id.seek(0,0)
    # 判断新的goods_id是否已存在在文件中,如果存在则不写入，不存在则写入
    goods_id_off = True
    for old_goods_id in all_goods_id.readlines():
        if goods_id.strip() in old_goods_id.strip():
            print('在all_goods_id文件中已存在！')
            goods_id_off = False
            break
    if goods_id_off:
        print('all_goods_id文件中不存在！')
        all_goods_id.write(str(goods_id))
        all_goods_id.write('\n')
    all_goods_id.close()

def save_lists():
    headers = ['url', 'goods_id', 'mall_id', 'jiage', 'goods_url']
    with open('pdd.csv', 'a+', encoding='utf-8', newline='') as f: # 如果出现多余空行，则使用newline参数将值设为空
        w = csv.DictWriter(f, headers) # 第2个参数，传递表头数据
        #w.writeheader() # 写入表头数据
        w.writerows(lists)
        print(f'低于1元的商品共找到：{len(lists)}')

def main():
    # 待解析json原数据
    files = r'D:\python\pdd\json'
    jsons = os.listdir(files)
    for f in jsons:
        json_file = files+ '\\' + f
        qingnxi_data(json_file)
        # break

if __name__ == '__main__':
    main()





